using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_FacilityBookingManagement : admin_BaseUserControl
{
    FacilityBooking fb = new FacilityBooking();
    protected void Page_Load(object sender, EventArgs e)
    {
        ActiveAccording = "booking";
        if (Request["id"] == null) return;
        try
        {
            int id = int.Parse(Request["id"]);
            CurrentID = Request["id"];
            Person per = new Person();
            Booking bk = new Booking();
            BookingDAO daoBooking = new BookingDAO();
            FacilityBookingDAO daoFB = new FacilityBookingDAO();
            PersonDAO daoPerson = new PersonDAO();
            //init
            cmbState.Items.Add(new ListItem("Booked","0"));
            cmbState.Items.Add(new ListItem("Accepted", "1"));
            cmbState.Items.Add(new ListItem("Canceled", "2"));
            //load
            fb = daoFB.GetFacilityBookingByBID(id);
            bk = daoBooking.GetBooking(fb.BookingID);
            fb.BookingID = bk.BookingID;


            //get personal information

            per = daoPerson.GetPerson(bk.CustomerID);
            lblCustomerName.Text = per.FirstName + " " + per.LastName;
            lblAddress.Text = per.Address;
            lblGender.Text=(per.Gender?"Male":"Female");
            lblEmail.Text = per.Email;
            lblPhone.Text = per.Phone;
            lblDateOfBirth.Text = per.DateOfBirth.ToString("MMM, dd yyyy");
            txtDuring.Text = fb.During.ToString();
            txtNote.Text = fb.Note;
            txtQuantity.Text = fb.Quantity.ToString();
            txtRentDate.Text = fb.RentTime.ToString("MM/dd/yyyy");
            lblBookingDate.Text = bk.ModifiedDate.ToString("MMM, dd yyyy - hh:mm tt");
            lblBasicPrice.Text = fb.Price.ToString("#.0");
            cmbState.SelectedIndex = (int)bk.State;
            //get facility information
            FacilityDAO daoFac = new FacilityDAO();
            Facility fac = daoFac.GetFacility(fb.Facility.Id);
            lblFacName.Text = fac.Name;
            

        }
        catch (Exception ex)
        {
            ShowMessage("Error. Booking details cannot be loaded");
        }

    }
    protected void lnkUpdate_Click(object sender, EventArgs e)
    {
        if (fb.ID <= 0)
        {
            ShowMessage("Error when trying to update the booking: Data cannot be loaded");
            return;
        }
        try
        {
            BookingDAO daoBooking = new BookingDAO();
            FacilityBookingDAO daoFB = new FacilityBookingDAO();
            fb.Quantity = int.Parse(txtQuantity.Text);
            fb.RentTime = DateTime.Parse(txtRentDate.Text);
            fb.During = int.Parse(txtDuring.Text);
            fb.State = (byte)cmbState.SelectedIndex;
            fb.Note = txtNote.Text;
            if (daoFB.Update(fb) > 0)
            {
                if (daoBooking.UpdateState(fb.BookingID,(byte)cmbState.SelectedIndex) > 0)
                {
                    ShowMessage("Booking is updated");
                }
                else ShowMessage("Error. Booking cannot be updated");
            }
            else ShowMessage("Error. Booking cannot be updated");
        }
        catch (Exception ex)
        {
            ShowMessage("Error. Information cannot be updated");
        }
    }
    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        if (fb.ID <= 0)
        {
            ShowMessage("Error when trying to update the booking: Data cannot be loaded");
            return;
        }
        BookingDAO daoBooking = new BookingDAO();
        if (daoBooking.Delete(fb.BookingID) > 0)
        {
            Response.Redirect("Default.aspx?ctrl=booking_list");
        }
        else ShowMessage("Error occured. Booking is not deleted.");
    }
    /// <summary>
    /// Return request string for search in list page
    /// </summary>
    public string GetSearchRequest()
    {
        if (Request["search"] != null) return Request["search"];
        return "";
    }
}
